﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace YXShop.Web.admin.templates.label
{
    public partial class freedomlabel_edit : System.Web.UI.Page
    {
        protected string TabList1 = "";
        protected string TabList2 = "";
        protected string JoinFlds1 = "";
        protected string JoinFlds2 = "";
        protected int id = 0;
        protected string stylecon = "";
        protected string descrpt = "";
        protected string lblname = "";
        protected string List1 = "";
        protected string List2 = "";
        protected string TopNum = "";
        protected string lblsql = "";

        protected void Page_Load(object sender, EventArgs e)
        {
          
            if (!Page.IsPostBack)
            {
                YXShop.Common.PromptInfo.Popedom("011003002","对不起，您没有权限进行编辑");
                YXShop.Common.PromptInfo.Popedom("011003004","对不起，您没有权限进行编辑");
                #region ajax
                if (Request.Form["Option"] != null && Request.Form["Option"].Equals("GetFields") && Request.Form["TableName"] != null && !Request.Form["TableName"].Equals(""))
                {

                    YXShop.BLL.Templates.FreeLabel freeBll = new YXShop.BLL.Templates.FreeLabel();
                    IList<YXShop.Model.Templates.FreeLabel.FreeLablelDBInfo> flds = freeBll.GetFields(Request.Form["TableName"]);
                    int i = 0;
                    foreach (YXShop.Model.Templates.FreeLabel.FreeLablelDBInfo flinfo in flds)
                    {
                        if (i > 0)
                            Response.Write(";");
                        Response.Write(flinfo.Name + "," + flinfo.TypeName);
                        i++;
                    }
                    Response.End();
                }
                #endregion 
                string tab1 = "", tab2 = "";
                YXShop.BLL.Templates.FreeLabel fb=new YXShop.BLL.Templates.FreeLabel();
                //判断是否模版进来修改的，是的话标签名不能修改 为只读
                if (Request["type"] != null && Request["type"].ToString().Equals("0"))
                {
                    this.TxtName.ReadOnly = true;
                }
                else
                {
                    this.TxtName.ReadOnly = false;
                }
                if (Request["id"] != null && !Request["id"].Trim().Equals(""))
                {
                    id = int.Parse(Request.QueryString["id"]);
                    YXShop.Model.Templates.FreeLabel ln=fb.GetSingle(id);
                    stylecon = ln.StyleContent;
                    descrpt = ln.Description;
                    lblsql = ln.LabelSql;
                    lblname = ln.LabelName.Replace("{#YXShopFreeTag*", "").Replace("#}", "");
                    this.TxtName.Text = lblname;
                    string pattern = @"select\s(top\s(?<tp>\d+)\s)?(?<flds>.+?)\sfrom\s(?<tb1>[^\s,]+)(,(?<tb2>\S+))?(\s?where\s(?<con>.+))?";
                    if (lblsql.ToLower().IndexOf(" order by ") > 0)
                        pattern += @"\sorder\sby\s(?<odr>.+)";
                    Regex reg = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    Match m = reg.Match(lblsql);
                    if (m.Success)
                    {
                        string con = "", ord = "";
                        string flds = m.Groups["flds"].Value.Trim();
                        tab1 = m.Groups["tb1"].Value.Trim();
                        if (m.Groups["tp"] != null) TopNum = m.Groups["tp"].Value.Trim();
                        if (m.Groups["tb2"] != null) tab2 = m.Groups["tb2"].Value.Trim();
                        if (m.Groups["con"] != null) con = m.Groups["con"].Value.Trim();
                        if (m.Groups["odr"] != null) ord = m.Groups["odr"].Value.Trim();
                        bool flagdbl = false;
                        string jnfld1 = "", jnfld2 = "";
                        if (!tab1.Equals("") && !tab2.Equals(""))
                        {
                            flagdbl = true;
                            string profile = tab1 + @"\.(?<jn1>[^=]+)\s?=\s?" + tab2 + @"\.(?<jn2>\S+)";
                            Regex regex = new Regex(profile, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                            Match match = regex.Match(con);
                            if (match.Success)
                            {
                                jnfld1 = match.Groups["jn1"].Value.Trim();
                                jnfld2 = match.Groups["jn2"].Value.Trim();
                                con = regex.Replace(con, "");
                            }
                        }
                        if (!tab1.Equals(""))
                        {
                            string option = "";
                            List1 = GetFields(fb, tab1, flds, con, ord, flagdbl, jnfld1, out option);
                            JoinFlds1 = option;
                        }
                        if (!tab2.Equals(""))
                        {
                            string option = "";
                            List2 = GetFields(fb, tab2, flds, con, ord, flagdbl, jnfld2, out option);
                            JoinFlds2 = option;
                        }                       
                    }
                }
                else
                {   
                    //用户在模版中定义的标签名但是数据库没有 点修改 就添加
                    if (Request["pe"] != null && Request["pe"].ToString().Equals("2"))
                    {
                        string strName = Request["LN"].ToString();
                        this.TxtName.Text = strName;
                       
                    }
                    else
                    {
                        this.TxtName.Text = lblname;
                    }
                }
               
                IList<YXShop.Model.Templates.FreeLabel.FreeLablelDBInfo> tbs = fb.GetTables();
                foreach (YXShop.Model.Templates.FreeLabel.FreeLablelDBInfo info in tbs)
                {
                    TabList1 += "<option";
                    if (info.Name.Equals(tab1))
                        TabList1 += " selected=\"selected\"";
                    TabList1 += " value=\"" + info.Name + "\">" + info.Description + "</option>\r";
                    TabList2 += "<option";
                    if (info.Name.Equals(tab2))
                        TabList2 += " selected=\"selected\"";
                    TabList2 += " value=\"" + info.Name + "\">" + info.Description + "</option>\r";

                }
                DataBind();
                
            }
           
        }
        private string GetFields(YXShop.BLL.Templates.FreeLabel fb, string tabnm, string fdlst, string cond, string ordr, bool dbflag, string jnfld, out string opt)
        {
            string ret = "";
            opt = "";
            IList<YXShop.Model.Templates.FreeLabel.FreeLablelDBInfo> fds = fb.GetFields(tabnm);
            foreach (YXShop.Model.Templates.FreeLabel.FreeLablelDBInfo info in fds)
            {
                opt += "<option";
                if (info.Name == jnfld)
                    opt += " selected=\"selected\"";
                opt += " value=\"" + info.Name + "\">" + info.Name + "</option>\r";
                string fld = info.Name;
                if (dbflag)
                    fld = tabnm + "." + info.Name;
                ret += "<tr style=\"cursor:hand\" onclick=\"RowClick(this)\" height=\"22\">\r";
                ret += "<td>" + info.Name + "</td>\r";
                ret += "<td>" + info.TypeName + "</td>\r<td align=\"center\">";
                if (fdlst.IndexOf(fld) >= 0)
                    ret += "√";
                ret += "</td>\r<td align=\"center\">";
                if (cond.IndexOf(fld) >= 0)
                {
                    string pattern = fld + @"\s?(?<con>\S+\s*\S+)";
                    Regex reg = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                    Match m = reg.Match(cond);
                    if (m.Success)
                    {
                        string con = m.Groups["con"].Value.Trim();
                        ret += con;
                    }
                }
                ret += "</td>\r<td align=\"center\">";
                if (ordr.IndexOf(fld + " ASC") >= 0)
                    ret += "升序";
                else if (ordr.IndexOf(fld + " DESC") >= 0)
                    ret += "降序";
                ret += "</td>\r</tr>";
            }
            return ret;
         }

        protected void TxtName_TextChanged(object sender, EventArgs e)
        {
            YXShop.BLL.Templates.FreeLabel freeBll = new YXShop.BLL.Templates.FreeLabel();
            string labelName = this.TxtName.Text.Trim().ToString();
            int id = 0;
            if (ChangeHope.WebPage.PageRequest.GetQueryString("id") != "")
            {
                id = int.Parse(ChangeHope.WebPage.PageRequest.GetQueryString("id"));
            }         
            if (freeBll.IsRepeat(labelName, id) == true)
            {
                this.ltlMsg.Text = "已经存在该标签名";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionErr";
                return;
            }
            else
            {
                this.pnlMsg.Visible = false;                
            }           
        }   
    }
}
